package com.fairytask.screen
{
	import com.fairytask.define.AssetsDefine;
	import com.fairytask.items.Bubble;
	
	import flash.geom.Point;
	
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.utils.AssetManager;

	/**
	 * 战斗场景 
	 * @author game
	 * 
	 */	
	public class BattleScreen extends Sprite
	{
		private static var _instance	:BattleScreen;
		//加载器
		private var assetsMgr			:AssetManager;
		//显示层
		private var uiFrame			:Image;
		private var itemSpace		:Sprite;
		private var isInit:Boolean;
		public function BattleScreen()
		{
			super();
			this.addEventListener(Event.RESIZE, onStageResize);
			initWorld();
			initEvent();
		}
		
		private function onStageResize(evt:Event):void
		{
			if(null!=uiFrame)
			{
				uiFrame.width	=	stage.stageWidth;
				uiFrame.height	=	stage.stageHeight;
				uiFrame.readjustSize();
			}
		}
		
		/**
		 * 初始化战斗的世界 
		 */
		private function initWorld():void
		{
			assetsMgr	=	new AssetManager();
			//==
			itemSpace	=	new Sprite();
			addChild(itemSpace);
			//开始加载
			assetsMgr.enqueue(AssetsDefine.BATTLE_IMG+"frame.jpg");
			assetsMgr.enqueue(AssetsDefine.BATTLE_MODEL+"popBasic.png");
			assetsMgr.enqueue(AssetsDefine.BATTLE_MODEL+"popSheet.xml",AssetsDefine.BATTLE_MODEL+"popSheet.png");
			assetsMgr.loadQueue(onProgress);
		}
		
		public static function get instance():BattleScreen
		{
			if(null==_instance)
			{
				_instance	=	new BattleScreen();
			}
			return _instance;
		}
		
		/**
		 * 事件处理 
		 * 
		 */
		private function initEvent():void
		{
			
		}
		
		/**
		 * 加载状态 
		 * @param pRatio
		 * 
		 */
		private function onProgress(pRatio:Number):void
		{
			if(pRatio==1)
			{
				uiFrame	=	new Image(assetsMgr.getTexture("frame"));
				uiFrame.width	=	stage.stageWidth;
				uiFrame.height	=	stage.stageHeight;
				addChildAt(uiFrame,0);
				
				isInit	=	true;
				//测试用
				showBattle(0,10000);
			}
		}

		/**
		 * 显示战斗 
		 * @param pType
		 * @param pMap
		 * 
		 */
		public function showBattle(pType:int,pMap:int):void
		{
			if(!isInit)return;
			//根据地图ID加载不同布局 对应不同背景
			var bubbleArr	:Array	=	[3,5,6,2,3,7,8,40,21,5];
			for (var i:int = 0; i < bubbleArr.length; i++) 
			{
				//new h
				var tmpBubbles	:Bubble	=	new Bubble(i,assetsMgr.getTexture("popBasic"));//assetsMgr.getTextureAtlas("popSheet").getTexture(bubbleArr[i])
				tmpBubbles.x	=	stage.stageWidth*Math.random();
				tmpBubbles.y	=	(stage.stageHeight-200)*Math.random();
				var preBubbles		:Bubble;
				if(i>0)
				{
					preBubbles	=	itemSpace.getChildAt(i-1) as Bubble;
					if(null!=tmpBubbles.hitTest(new Point(preBubbles.x,preBubbles.y)))
					{
						tmpBubbles.x	=	stage.stageWidth*Math.random();
						tmpBubbles.y	=	(stage.stageHeight-200)*Math.random();
					}
				}
				itemSpace.addChild(tmpBubbles);
			}
			
		}
	}
}